//
//  HXDFamousCollectionViewCell.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/26.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDFamousTableViewCell.h"
#import "HXDFamousCell.h"
#import "HXDHomeDataModel.h"
#import "HXDShopInfoTableViewController.h"

@interface HXDFamousTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic) HXDFamousCell *famousCell;
@property (strong, nonatomic) HXDHomeDataModel *famousData;

@end

@implementation HXDFamousTableViewCell

+ (instancetype)famousTableViewCell {
    return [[NSBundle mainBundle] loadNibNamed:@"HXDFamousTableViewCell" owner:nil options:nil].lastObject;
}

static NSString *ID = @"famousCell";

- (void)awakeFromNib {
//    self.collectionView.bounces = NO;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HXDFamousCell" bundle:nil] forCellWithReuseIdentifier:ID];
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataWithNotify:) name:@"data" object:nil];
}

//在这个方法中设置流水布局样式
- (void)layoutSubviews {
    [super layoutSubviews];
    self.flowLayout.itemSize = CGSizeMake(187, 100);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsZero;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (void)getDataWithNotify:(NSNotification *)notify {
    self.famousData = notify.object;
    //获取到数据之后一定要重新刷新界面
    [self.collectionView reloadData];
}

//每组多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.famousData.home.famousList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXDFamousCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.famousData = self.famousData.home.famousList[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"jump" object:nil];
}

@end
